﻿using Acme.BookStore.AcmeModel;
using Acme.BookStore.AcmeModel.Model;
using Acme.BookStore.Application;
using Acme.BookStore.Credit;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    /// <summary>
    /// 合同管理增删改查
    /// </summary>
    public class CreditServe :
        CrudAppService<
            CreditInfo, //The Book entity
            CreditInfoDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            CreditInfos>, //Used to create/update a book
        ICreditInfo //implement the IBookAppService
    {
        public IRepository<CreditInfo, Guid> _Creditor;
        public IRepository<LineInfo, Guid> _Line;
        public IRepository<LogsInfo, Guid> _Logs;
        public CreditServe(IRepository<CreditInfo, Guid> Creditor, IRepository<LineInfo, Guid> Line, IRepository<LogsInfo, Guid> Logs)
            : base(Creditor)
        {
            _Creditor = Creditor;
            _Line = Line;
            _Logs = Logs;
        }
        /// <summary>
        /// 货主合同管理
        /// </summary>
        /// <returns></returns>
        [Route("合同管理显示")]
        //货主合同显示
        public List<CreditInfoDto> GetShow(string Aaa = "", string Bbb = "", string Ccc = "", string Ddd = "")
        {
            var str = (from a in _Creditor
                       select new CreditInfoDto
                       {
                           Id = a.Id,
                           CreditoId = a.CreditoId,
                           CreditoNumber = a.CreditoNumber,
                           CreditocTitle = a.CreditocTitle,
                           CreditocCompany = a.CreditocCompany,
                           CreditoRow = a.CreditoRow,
                           CreditoPrix = a.CreditoPrix,
                           CreditoBiz = a.CreditoBiz,
                           CreditoClauseBiz = a.CreditoClauseBiz,
                           CreditoBmoney = a.CreditoBmoney,
                           CreditoMoney = a.CreditoMoney,
                           CreditoNow = a.CreditoNow,
                           CreditoTime = a.CreditoTime,
                           CreditoType = a.CreditoType,
                           CreditoRowNow = a.CreditoRowNow
                       }).ToList();
            //文本
            if (!string.IsNullOrEmpty(Aaa))
            {
                str = str.Where(x => x.CreditocTitle.Contains(Aaa)).ToList();
            }
            if (!string.IsNullOrEmpty(Bbb))
            {
                str = str.Where(x => x.CreditocCompany.Contains(Bbb)).ToList();
            }
            if (!string.IsNullOrEmpty(Ccc))
            {
                str = str.Where(x => x.CreditoRow.Contains(Ccc)).ToList();
            }
            if (!string.IsNullOrEmpty(Ddd))
            {
                str = str.Where(x => x.CreditoNow.Contains(Ddd)).ToList();
            }
            return str;
        }
        [Route("贷主合同添加")]
        [HttpPost]
        //贷主合同添加
        public async Task<int> GetAdd(CreditInfos m)
        {
            CreditInfo str = ObjectMapper.Map<CreditInfos, CreditInfo>(m);
            if (str!=null)
            {
                await _Creditor.InsertAsync(str);
                var Ps = "";
                var Pt = "";
                AppLog.WriteInfo("添加成功", true);
                AppLog.WriteError("添加成功", false);
                AppLog.WriteWarn("添加成功", true);
                Ps = "添加操作";
                Pt = "添加成功";
                NlogShow(Ps, Pt);
                return 1;
            }
            else
            {
                await _Creditor.InsertAsync(str);
                var Ps = "";
                var Pt = "";
                AppLog.WriteInfo("添加失败", true);
                AppLog.WriteError("添加失败", true);
                AppLog.WriteWarn("添加失败", true);
                Ps = "添加操作";
                Pt = "添加成功";
                NlogShow(Ps, Pt);
                return 2;
            }
        }
        //无返回值方法（目的:执行日志添加操作）
        private async void NlogShow(string Ps, string Pt)
        {
            LogsInfo log = new LogsInfo();
            log.NlogTreay = Pt;
            log.NlogPsuin = Ps;
            log.NlogMsg = 1;
            log.NlogTime = DateTime.Now;
            await _Logs.InsertAsync(log);
        }

        [Route("贷主合同删除")]
        [HttpPost]
        //贷主合同删除
        public async Task<int> GetDel(Guid Id)
        {
            await _Creditor.DeleteAsync(Id);
            return 1;
        }
        [Route("贷主合同反填")]
        //贷主合同反填
        public async Task<CreditInfoDto> GetFt(Guid Id)
        {
            CreditInfo str = await _Creditor.GetAsync(Id);
            return ObjectMapper.Map<CreditInfo, CreditInfoDto>(str);
        }
        [Route("贷主合同修改")]
        [HttpPost]
        //贷主合同修改
        public async Task<int> GetUpd(CreditInfoDto m)
        {
            CreditInfo Str = await _Creditor.GetAsync(m.Id);
            Str.CreditoId = m.CreditoId;
            Str.CreditoNumber = m.CreditoNumber;
            Str.CreditocTitle = m.CreditocTitle;
            Str.CreditocCompany = m.CreditocCompany;
            Str.CreditoRow = m.CreditoRow;
            Str.CreditoPrix = m.CreditoPrix;
            Str.CreditoBiz = m.CreditoBiz;
            Str.CreditoClauseBiz = m.CreditoClauseBiz;
            Str.CreditoBmoney = m.CreditoBmoney;
            Str.CreditoMoney = m.CreditoMoney;
            Str.CreditoNow = m.CreditoNow;
            Str.CreditoTime = m.CreditoTime;
            Str.CreditoType = m.CreditoType;
            Str.CreditoRowNow = m.CreditoRowNow;
            Str.CreditPmoneys = m.CreditPmoneys;
            Str.CreditSmoney = m.CreditSmoney;
            Str.CreditTmoneys = m.CreditTmoneys;
            return 1; 
        }
        [Route("贷主合同修改状态（提交审批）")]
        //合同管理修改状态（提交审批）
        public async Task<int> GetYupdState(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 2;
            return 1;
        }
        [Route("贷主合同修改状态（重新提交）")]
        //贷主合同修改状态（重新提交）
        public async Task<int> GetEupdState(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 1;
            return 1;
        }
        [Route("贷主合同审批通过")]
        //贷主合同审批通过
        public async Task<int> GetTg(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 3;
            Str.CreditoRowNow = "小王";
            return 1;
        }
        [Route("贷主合同审批拒绝")]
        //贷主合同审批拒绝
        public async Task<int> GetJj(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 4;
            Str.CreditoRowNow = "小王";
            return 1;
        }
        /// <summary>
        /// 承运合同管理
        /// </summary>
        /// <returns></returns
        [Route("合同审批显示")]
        //承运合同显示
        public List<CreditInfoDto> GetPShow(string Aaa = "", string Bbb = "", string Ccc = "", string Ddd = "")
        {
            var str = (from a in _Creditor
                       select new CreditInfoDto
                       {
                           Id = a.Id,
                           CreditoId = a.CreditoId,
                           CreditoNumber = a.CreditoNumber,
                           CreditocTitle = a.CreditocTitle,
                           CreditocCompany = a.CreditocCompany,
                           CreditoRow = a.CreditoRow,
                           CreditoPrix = a.CreditoPrix,
                           CreditoBiz = a.CreditoBiz,
                           CreditoClauseBiz = a.CreditoClauseBiz,
                           CreditoBmoney = a.CreditoBmoney,
                           CreditoMoney = a.CreditoMoney,
                           CreditoNow = a.CreditoNow,
                           CreditoTime = a.CreditoTime,
                           CreditoType = a.CreditoType,
                           CreditoRowNow = a.CreditoRowNow
                       }).Where(a => a.CreditoType.Equals(2)).ToList();
            //文本
            if (!string.IsNullOrEmpty(Aaa))
            {
                str = str.Where(x => x.CreditocTitle.Contains(Aaa)).ToList();
            }
            if (!string.IsNullOrEmpty(Bbb))
            {
                str = str.Where(x => x.CreditocCompany.Contains(Bbb)).ToList();
            }
            if (!string.IsNullOrEmpty(Ccc))
            {
                str = str.Where(x => x.CreditoRow.Contains(Ccc)).ToList();
            }
            if (!string.IsNullOrEmpty(Ddd))
            {
                str = str.Where(x => x.CreditoNow.Contains(Ddd)).ToList();
            }

            return str;
        }
        [Route("承运合同添加")]
        [HttpPost]
        //承运合同管理添加
        public async Task<int> GetPAdd(CreditInfos m)
        {
            CreditInfo str = ObjectMapper.Map<CreditInfos, CreditInfo>(m);
            await _Creditor.InsertAsync(str);
            return 1;
        }
        [Route("承运合同删除")]
        [HttpPost]
        //承运合同删除
        public async Task<int> GetPDel(Guid Id)
        {
            await _Creditor.DeleteAsync(Id);
            return 1;
        }



        [Route("承运合同反填")]
        //承运合同反填
        public async Task<CreditInfoDto> GetPFt(Guid Id)
        {
            CreditInfo str = await _Creditor.GetAsync(Id);
            return ObjectMapper.Map<CreditInfo, CreditInfoDto>(str);
        }
        [Route("承运合同修改")]
        [HttpPost]
        //承运合同修改
        public async Task<int> GetPUpd(CreditInfoDto m)
        {
            CreditInfo Str = await _Creditor.GetAsync(m.Id);
            Str.CreditoId = m.CreditoId;
            Str.CreditoNumber = m.CreditoNumber;
            Str.CreditocTitle = m.CreditocTitle;
            Str.CreditocCompany = m.CreditocCompany;
            Str.CreditoRow = m.CreditoRow;
            Str.CreditoPrix = m.CreditoPrix;
            Str.CreditoBiz = m.CreditoBiz;
            Str.CreditoClauseBiz = m.CreditoClauseBiz;
            Str.CreditoBmoney = m.CreditoBmoney;
            Str.CreditoMoney = m.CreditoMoney;
            Str.CreditoNow = m.CreditoNow;
            Str.CreditoTime = m.CreditoTime;
            Str.CreditoType = m.CreditoType;
            Str.CreditoRowNow = m.CreditoRowNow;
            Str.CreditPmoneys = m.CreditPmoneys;
            Str.CreditSmoney = m.CreditSmoney;
            Str.CreditTmoneys = m.CreditTmoneys;
            return 1;
        }
        [Route("承运合同修改状态（提交审批）")]
        //承运合同修改状态（提交审批）
        public async Task<int> GetPYupdState(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 2;
            return 1;
        }
        [Route("承运合同修改状态（重新提交）")]
        //承运合同修改状态（重新提交）
        public async Task<int> GetPEupdState(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 1;
            return 1;
        }
        [Route("承运合同审批通过")]
        //承运合同审批通过
        public async Task<int> GetPTg(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 3;
            Str.CreditoRowNow = "小王";
            return 1;
        }
        [Route("承运合同审批拒绝")]
        //承运合同审批拒绝
        public async Task<int> GetPJj(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            Str.CreditoType = 4;
            Str.CreditoRowNow = "小王";
            return 1;
        }
        /// <summary>
        /// 承运跟踪管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("承运跟踪显示")]
        ///承运跟踪显示
        public List<CreditInfoDto> Show(string Aaa = "", string Bbb = "", string Ccc = "", string Ddd = "")
        {
            var str = (from a in _Creditor
                       join b in _Line
                       on a.CreditWj equals b.Id.ToString()
                       select new CreditInfoDto
                       {
                           Id = a.Id,
                           CreditoId = a.CreditoId,
                           CreditoNumber = a.CreditoNumber,
                           CreditocTitle = a.CreditocTitle,
                           CreditocCompany = a.CreditocCompany,
                           CreditoRow = a.CreditoRow,
                           CreditoPrix = a.CreditoPrix,
                           CreditoBiz = a.CreditoBiz,
                           CreditoClauseBiz = a.CreditoClauseBiz,
                           CreditoBmoney = a.CreditoBmoney,
                           CreditoMoney = a.CreditoMoney,
                           CreditoNow = a.CreditoNow,
                           CreditoTime = a.CreditoTime,
                           CreditoType = a.CreditoType,
                           LineType = b.LineType
                       }).ToList();
            //文本
            if (!string.IsNullOrEmpty(Aaa))
            {
                str = str.Where(x => x.CreditocTitle.Contains(Aaa)).ToList();
            }
            if (!string.IsNullOrEmpty(Bbb))
            {
                str = str.Where(x => x.CreditocCompany.Contains(Bbb)).ToList();
            }
            if (!string.IsNullOrEmpty(Ccc))
            {
                str = str.Where(x => x.CreditoRow.Contains(Ccc)).ToList();
            }
            if (!string.IsNullOrEmpty(Ddd))
            {
                str = str.Where(x => x.CreditoNow.Contains(Ddd)).ToList();
            }
            return str;
        }
        //承运跟踪签收
        [Route("承运跟踪签收")]
        public async Task<int> GetQs(Guid id)
        {
            try
            {
                CreditInfo Str = await _Creditor.GetAsync(id);
                if (Str.CreditoType == 2)
                {
                    Str.CreditoType = 3;
                    return 1;
                }
                else
                {
                    return 2;
                }
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        [Route("承运跟踪发货")]
        //承运跟踪发货
        public async Task<int> GetFh(Guid id)
        {
            CreditInfo Str = await _Creditor.GetAsync(id);
            if (Str.CreditoType == 1)
            {
                Str.CreditoType = 2;
                return 1;
            }
            else
            {
                return 2;
            }
        }
        [Route("承运跟踪回单")]
        //承运跟踪回单
        public async Task<int> GetHd(Guid id)
        {
            
            CreditInfo Str = await _Creditor.GetAsync(id);
            if (Str.CreditoType == 3)
            {
                Str.CreditoType = 2;
                return 1;
            }
            else
            {
                return 2;
            }
        }
        [HttpPost]
        [Route("承运跟踪删除")]
        //承运跟踪删除
        public async Task<int> GetGzDel(Guid id)
        {
            await _Creditor.DeleteAsync(id);
            return 1;
        }
    }
}













            